<?php


namespace app\admin\controller;


use app\admin\model\Announcement;
use app\admin\model\Approval;
use app\admin\model\AuthGroup;
use app\admin\model\Item;
use app\admin\model\WorkJournal;
use app\admin\model\WorkRun;
use app\BaseController;
use app\Request;
use fast\Tree;
use think\facade\Db;
use think\facade\View;

class Home extends BaseController
{
    /**
     * @var \app\admin\model\Admin
     */
    protected $childrenGroupIds = [];
    protected $childrenAdminIds = [];
    protected $group = [];
    protected $role_id;
    protected $groupList = "";

    public function initialize()
    {
        parent::initialize();

        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
        $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);

        $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
        $this->groupList = collection(AuthGroup::select())->toArray();
        Tree::instance()->init($groupList);
        $groupdata = [];
        if ($this->auth->isSuperAdmin()) {
            $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
            foreach ($result as $k => $v) {
                $groupdata[$v['id']] = $v['name'];
            }
        } else {
            $result = [];
            $groups = $this->auth->getGroups();
            foreach ($groups as $m => $n) {
                $this->role_id = $n['id'];
                $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
                $temp = [];
                foreach ($childlist as $k => $v) {
                    $temp[$v['id']] = $v['name'];
                }
                $result[($n['name'])] = $temp;
            }
            $groupdata = $result;
        }
        $this->group = $groupdata;
    }

    public function index()
    {
        //总人数
        $admin = \app\admin\model\Admin::count();
        //总文件数
        $file = \app\admin\model\File::where('file_type', 'file')->count();
        $today_file = \app\admin\model\File::where('file_type', 'file')->whereDay('create_time')->count();
        //总项目
        $project = Item::count();
        $today_project = Item::whereDay('create_time')->count();
        //总客户
        $customer = \app\admin\model\Customer::count();
        $today_customer = \app\admin\model\Customer::whereDay('create_time')->count();
        //系统公告
        $system = Announcement::order(['create_time' => 'desc'])->limit(10)->select();
        //待审批
        $list = Db::name('view_workitem')
            ->where('status', '0')
            ->where('receiveid', $this->auth->id)
            ->select();
        View::assign('admin', $admin);
        View::assign('file', $file);
        View::assign('result', $list);
        View::assign('system', $system);
        View::assign('project', $project);
        View::assign('customer', $customer);
        View::assign('today_customer', $today_customer);
        View::assign('today_project', $today_project);
        View::assign('today_file', $today_file);


        return View::fetch('index/console');
    }

    public function check_content(Request $request)
    {
        $id = $request->param('id');
        $system = Announcement::find($id);
        View::assign('system', $system);
        return View::fetch('index/check_content');

    }


    //待审核日志
    public function journal(Request $request)
    {
        if ($request->isAjax()) {
            //获取每页显示的条数
            $limit = $request->param('limit');
            //获取当前页数
            $page = $request->param('page');
            $total = Db::name('view_workitem')
                ->where('status', '0')
                ->where('receiveid', $this->auth->id)
                ->count();
            $list = Db::name('view_workitem')
                ->where('status', '0')
                ->where('receiveid', $this->auth->id)
                ->order('createtime desc')
                ->page($page,$limit)
                ->select();
            return json(['code' => 200, 'msg' => '', 'total' => $total, 'data' => $list]);
        }
    }

    /**
     * 得到当前位置所有父辈数组
     * @param int
     * @param bool $withself 是否包含自己
     * @return array
     */
    public function getParent($arr, $myid)
    {
        $pid = 0;
        $newarr = [];
        foreach ($arr as $value) {
            if (!isset($value['id'])) {
                continue;
            }
            if ($value['id'] == $myid) {
                $pid = $value['pid'];
                break;
            }
        }
        if ($pid) {
            foreach ($arr as $value) {
                if ($value['id'] == $pid) {
                    $newarr[] = $value;
                    break;
                }
            }
        }
        return $newarr;
    }

}